﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class SearchList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            return;
        }
        if (Session["Search"] == null || Session["Search"].ToString().Equals("false"))
        {
            Response.Redirect("Default.aspx");
        }
        Session.Add("Search", "false");
        if (Session["SearchCatalog"].ToString().Equals("0"))
        {
            string commandstring="";
            commandstring += "SELECT News.News_id, News.Title, News.Time, NewsToCatalog.Catalog_id, News.isPublished, Catalog.CatalogName FROM News INNER JOIN NewsToCatalog ON News.News_id = NewsToCatalog.News_id INNER JOIN Catalog ON NewsToCatalog.Catalog_id = Catalog.Catalog_id WHERE (News.isPublished = @isPublished) AND (News.Title like '%";
            commandstring += Session["SearchKeyWord"].ToString().Trim();
            commandstring += "%') ORDER BY News.News_id DESC";
            NewsSqlDataSource.SelectCommand =commandstring;
        }
        else
        {
            string commandstring="";
            commandstring+="SELECT News.News_id, News.Title, News.Time, NewsToCatalog.Catalog_id, News.isPublished, Catalog.CatalogName FROM News INNER JOIN NewsToCatalog ON News.News_id = NewsToCatalog.News_id INNER JOIN Catalog ON NewsToCatalog.Catalog_id = Catalog.Catalog_id WHERE (News.isPublished = @isPublished) AND (NewsToCatalog.Catalog_id=";
            commandstring+=Session["SearchCatalog"].ToString();
            commandstring+=") AND (News.Title like '%";
            commandstring += Session["SearchKeyWord"].ToString().Trim();
            commandstring += "%') ORDER BY News.News_id DESC";
            NewsSqlDataSource.SelectCommand = commandstring;
        }
        ListHyperLink.Text = "搜索列表";
    }
    protected void NewsListGridView_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        Session.Add("News_id", ((Label)NewsListGridView.Rows[e.NewSelectedIndex].FindControl("News_id")).Text);
        string theCatalog = ((Label)NewsListGridView.Rows[e.NewSelectedIndex].FindControl("News_Catalog_id")).Text;
        switch (theCatalog)
        {
            case "1":
                Response.Redirect("PictureNews.aspx");
        	    break;
            case "2":
                Response.Redirect("CollegeNews.aspx");
                break;
            case "3":
                Response.Redirect("Notice.aspx");
                break;
            case "4":
                Response.Redirect("IndependentInstituteDynamic.aspx");
                break;
            case "5":
                Response.Redirect("CampusDynamic.aspx");
                break;
        }
    }
    protected void NewsListGridView_DataBound(object sender, EventArgs e)
    {
        FoundCountLabel.Text = "共搜索到" + NewsListGridView.Rows.Count.ToString() + "条目";
    }
    protected void NewsListGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton News_title = e.Row.FindControl("News_title") as LinkButton;
            News_title.Text = HtmlFormat.HtmlFormat.DeleteHtmlFormat(News_title.Text);
        }
    }
}
